<?php
declare(strict_types=1);

namespace app\common\controller;

use app\common\lib\ResponseJson;
use think\facade\Log;
use think\facade\Session;

/**
 * | Notes：Api
 * +----------------------------------------------------------------------
 * | PHP Version 7.2+
 * +----------------------------------------------------------------------
 * | Copyright (c) 2011-2020 https://www.xxq.com.cn, All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 和好 <389123492@qq.com>
 * +----------------------------------------------------------------------
 * | Date: 2020/5/20 10:35
 * +----------------------------------------------------------------------
 */
class Api extends AppBase
{
    use ResponseJson;

    /**
     * initialize 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 检测安全密码
     *
     * @param int    $uuid
     * @param string $safety_code
     *
     * @return bool
     */
    public static function checkSafetyCode(int $uuid, string $safety_code): bool
    {
        $check_nonce       = self::encryptionTool($uuid, $safety_code);
        $check_safety_code = hash("sha256", $check_nonce);
        if ($check_safety_code == self::$user_cache['safety_code']) return true;
        return false;
    }

    /**
     * 验证码错误
     *
     * @param string $mobile
     * @param string $verify_code
     *
     * @return bool
     */
    public static function checkVerifySms(string $mobile, string $verify_code = ''): bool
    {
        $smsauth = Session::has('sms_secret_key') ? Session::get('sms_secret_key') : 0;

        Log::record('【smsauth】参数' . json_encode($smsauth));
        if (!isset($smsauth['send_time']) || $smsauth['send_time'] < time()) return false;

        // 验证验证码
        if (!isset($smsauth['encryption']) || md5($mobile . $verify_code) !== $smsauth['encryption']) return false;

        // 赋值（当前作用域）
        Session::set('sms_secret_key', '');

        return true;
    }

    /**
     * 验证码错误
     *
     * @param string $mobile
     * @param string $verify_code
     *
     * @return bool
     */
    public static function checkVerifySmsNoClean(string $mobile, string $verify_code = ''): bool
    {
        $smsauth = Session::has('sms_secret_key') ? Session::get('sms_secret_key') : 0;

        Log::record('【smsauth】参数' . json_encode($smsauth));
        if (!isset($smsauth['send_time']) || $smsauth['send_time'] < time()) return false;

        // 验证验证码
        if (!isset($smsauth['encryption']) || md5($mobile . $verify_code) !== $smsauth['encryption']) return false;


        return true;
    }
}
